import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";
// 异步修改

const channelStore = createSlice({
  name: "channel",
  initialState: {
    channelList: [],
  },
  reducers: {
    setChannel: (state, action) => {
      state.channelList = action.payload;
    },
  },
});
const { setChannel } = channelStore.actions;

//异步请求
const fetchChannel = () => {
  return async (dispatch) => {
    const res = await axios.get("http://geek.itheima.net/v1_0/channels");
    dispatch(setChannel(res.data.data.channels));
  };
};

const reducer = channelStore.reducer;

export { fetchChannel };

export default reducer;
